﻿using Ivony.Html;
using Ivony.Html.Parser;
using RestSharp;
using System;
using System.IO;
using YouWei.Utility;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Linq;
using YouWei.Infrastructure.Models;

namespace YouWei.Console
{
    public class SpidersWxComment
    {
        public void excute()
        {
            Uri u = new Uri("https://mp.weixin.qq.com/s/YAhzuAUCRbykFjBvVsMzsg");
            var entity = new CommentDto();
            var TextListUrl = "/s/YAhzuAUCRbykFjBvVsMzsg";
            //IHtmlDocument source = new JumonyParser().LoadDocument(TextListUrl, System.Text.Encoding.GetEncoding("utf-8"));
            RestClient client = new RestClient("https://mp.weixin.qq.com");

            //var request = new RestRequest("/2/short_url/shorten.json?source=2849184197&url_long=" + HttpUtility.UrlEncode(linkurl), Method.GET);
            var request = new RestRequest(TextListUrl, Method.GET);
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            IHtmlDocument source = new JumonyParser().Parse(content);
            var imageDoms = source.Find("img");
            if (imageDoms != null)
            {
                var index = 1;
                foreach (var item in imageDoms)
                {
                    var srcDom = item.Attribute("src");
                    if (srcDom == null)
                        srcDom = item.Attribute("data-src");
                    if (srcDom == null)
                        continue;
                    string src = srcDom.AttributeValue;
                    if (!string.IsNullOrWhiteSpace(src))
                    {
                        string filePath = "C://Project//work//youwei//YouWei//YouWei.Op//js//umeditor//net//upload";

                        string dir = Path.GetDirectoryName(filePath);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        if (Path.GetExtension(src) == ".jpg")
                            return;
                        //替换掉//res.wx.qq.com/mmbizwap的//
                        src = Regex.Replace(src, "^//", "https://");
                        var img = ImageHelper.UrlToImage(src);
                        TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
                        var tsStr = ts.TotalMilliseconds.ToString();
                        var savePath = filePath + "//test" + index + tsStr + ".jpg";
                        using (Bitmap tempImage = new Bitmap(img))
                        {
                            tempImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);
                        }
                        //img.Save(savePath);
                        item.SetAttribute("src", savePath);
                        item.SetAttribute("data-src", savePath);
                        index += 1;
                    }
                }
                entity.CommentName = source.Find("h2").ToList().FirstOrDefault().InnerText();
                entity.CommentDesc = entity.CommentName;
                entity.CommentDesc = source.Find("#js_content").ToList().FirstOrDefault().InnerHtml();
            }
        }

    }
}